package fast.loader;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.HashMap;

import fast.data.StatBlock;


public class Equipment extends HashMap<Slot,ItemEquipped> {
	
	private static final long serialVersionUID = 1L;
	
	private PropertyChangeSupport changes = new PropertyChangeSupport( this );
	
	public Equipment(){
		//
	}
	
	public ItemEquipped put(Slot slot, ItemEquipped item) {
		ItemEquipped old = this.get(slot);
		super.put(slot, item);
		changes.firePropertyChange( slot.name(), old, item );
		return this.get(slot);
	}
	
	public void put(Slot slot,Item item){
		ItemEquipped _new = new ItemEquipped();
		_new.setItem(item);
		ItemEquipped old = this.get(slot);
		super.put(slot, _new);
		changes.firePropertyChange( slot.name(), old, _new );
	}
	
	public void setEnchant(Slot slot,Enchant e){
		Enchant old = this.get(slot).getEnchant();
		this.get(slot).setEnchant(e);
		changes.firePropertyChange( slot.name()+"_enchant", old, e);
	}
	
	public void setGem(Slot slot, int gemSlot, Gem gem) {
		Gem old = this.get(slot).getGem(gemSlot);
		this.get(slot).setGem(gemSlot, gem);
		changes.firePropertyChange( slot.name()+"_"+gemSlot+"_gem", old, gem);
	}
	
	public void setReforge(Slot slot, Reforge r) {
		Reforge old = this.get(slot).getReforge();
		this.get(slot).setReforge(r);
		changes.firePropertyChange( slot.name()+"_reforge", old, r);
	}
	
	public void setAutoReforge(Slot slot, Reforge r){
		this.get(slot).setReforge(r);
	}
	
	public void setUpgrade(Slot slot, int lvl) {
		ItemEquipped old = this.get(slot);
		this.put(slot, old);
		changes.firePropertyChange( slot.name()+"_upgrade", old, lvl);
	}
	
	public void fireAutoReforgeComplete() {
		changes.firePropertyChange("autoreforge", false, true);
	}
	
	public void fireAutoGemComplete() {
		changes.firePropertyChange("autogem", false, true);
	}
	
	public void addExtraSocket(Slot slot) {
		for(int i = 0;i < this.get(slot).getItem().gemSlot.length; i++){
			if(this.get(slot).getItem().gemSlot[i] == Socket.prismatic)
				break;
			else if(this.get(slot).getItem().gemSlot[i] == null){
				this.get(slot).getItem().gemSlot[i] = Socket.prismatic;
				break;
			}
		}
	}
	
	public void removeExtraSocket(Slot slot) {
		for(int i = 0;i < this.get(slot).getItem().gemSlot.length; i++){
			if(this.get(slot).getItem().gemSlot[i] == Socket.prismatic){
				this.get(slot).getItem().gemSlot[i] = null;
				this.get(slot).setGem(i, null);
			}
		}
	}
	
	public void fireProfessionChangeComplete() {
		changes.firePropertyChange("professionchange", false, true);
	}
	
	public StatBlock getStatsAsBlock(){
		StatBlock i = new StatBlock();
		for(Slot slot: this.keySet()){
			ItemEquipped ie = this.get(slot);
			i.add(ie.getItem());
			i.add(ie.getReforgeAsBlock());
			
			// gems
			boolean colorMatch = true;
			for(int j = 0; j < ie.getItem().gemSlot.length;j++){
				if(ie.getItem().gemSlot[j] == null) continue;
				if(ie.getGem(j) != null){
					i.add(ie.getGem(j));
					if(!doesSocketMatch(ie.getItem().gemSlot[j],ie.getGem(j)))
						colorMatch = false;
				}else if(ie.getItem().gemSlot[j] != Socket.prismatic){
					colorMatch = false;
				}
			}
			if(ie.getItem().getSocketBonus() != null && colorMatch){
				i.add(ie.getItem().getSocketBonus());
			}
			// enchant
			if(ie.getEnchant() != null){
				Enchant e = ie.getEnchant();
				i.add(e);
			}
		}
		return i;
	}
	
	public StatBlock getReforgingsAsBlock() {
		StatBlock i = new StatBlock();
		for(Slot slot: this.keySet()){
			Reforge r = this.get(slot).getReforge();
			if(r != null){
				i.add(this.get(slot).getReforgeAsBlock());
			}
		}
		return i;
	}
	
	public void addPropertyChangeListener(PropertyChangeListener l){
		changes.addPropertyChangeListener(l);
	}

	public void removePropertyChangeListener(PropertyChangeListener l){
		changes.removePropertyChangeListener(l);
	}
	
	public boolean doesSocketMatch(Socket slotColor,Gem gem){
		if(		slotColor == Socket.red && (gem.getType() == Gem.RED || gem.getType() == Gem.PURPLE || gem.getType() == Gem.ORANGE) ||
				slotColor == Socket.yellow && (gem.getType() == Gem.YELLOW || gem.getType() == Gem.ORANGE || gem.getType() == Gem.GREEN) ||
				slotColor == Socket.blue && (gem.getType() == Gem.BLUE || gem.getType() == Gem.PURPLE || gem.getType() == Gem.GREEN) ||
				slotColor == Socket.prismatic
			)
			return true;
		return false;
	}
}
